package guoanmaker.operator.business.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.dao.SecckillCommodityDao;
import guoanmaker.operator.business.model.OperatorSecckill;
import guoanmaker.operator.business.modelnobind.ActivityInfor;
import guoanmaker.operator.business.modelnobind.CommodityMove;
import guoanmaker.operator.business.modelnobind.SecckillAndCommodMove;
import guoanmaker.operator.business.modelnobind.SecckillMove;
import guoanmaker.operator.business.repository.OperatorSecckillRepository;
import guoanmaker.operator.helper.common.AjaxJson;

/**
 * @author wanghongwei 
 * @ClassName: OperatorSecckillService 
 * @Description: OperatorSecckillService
 * @date 2017/11/21
 */
@Service
public class OperatorSecckillService implements OperatorSecckillServiceInterface {
	private final OperatorSecckillRepository operatorSecckillRepository;
	private final SecckillCommodityDao secckillCommodityDao;
	private AjaxJson ajaxJson;

	public OperatorSecckillService(OperatorSecckillRepository operatorSecckillRepository,
			SecckillCommodityDao secckillCommodityDao) {
		this.operatorSecckillRepository = operatorSecckillRepository;
		this.secckillCommodityDao = secckillCommodityDao;
	}

	@Transactional
	@Override
	public OperatorSecckill saveOperatorSecckill(OperatorSecckill operatorSecckill) {
		CommodityMove CommodityMove;
		try {
			CommodityMove = secckillCommodityDao.selectByCommodityCode(operatorSecckill.getCommodCode());
			Double price = CommodityMove.getFinalPrice() - operatorSecckill.getSkPrice();
			operatorSecckill.setSkPriceDiffer(price);
			operatorSecckill.setCommodFinalPrice(CommodityMove.getFinalPrice());
			operatorSecckill.setCommodId(CommodityMove.getId());
			operatorSecckill.setSkSellNum(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return operatorSecckillRepository.save(operatorSecckill);
	}

	@Override
	public CommodityMove selectProvideCommodityByCode(Integer commodCode) {
		try {
			CommodityMove commodityMove = secckillCommodityDao.selectByCommodityCode(commodCode);
			return commodityMove;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void deleteOperatorSecckillById(String id) {
		operatorSecckillRepository.delete(id);
	}

	@Override
	public List<SecckillAndCommodMove> selectSecckillByDate(String activityId, String skStartTime) {
		Date skTime = null;
		if (skStartTime != null) {
			long msl = (long) Long.valueOf(skStartTime).longValue()* 1000;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				String str = sdf.format(msl);
				skTime = sdf.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		try {
			List<SecckillAndCommodMove> SecckillAndCommodList = secckillCommodityDao.selectSecckillAndCommod(activityId,
					skTime);
			return SecckillAndCommodList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ActivityInfor selectSecckillByActivityId(String activityId) {
		ActivityInfor activityInfor = secckillCommodityDao.selectActivityInforByActivityId(activityId);
		List<SecckillMove> secckilllist = secckillCommodityDao.selectSecckillByActivityId(activityId);
		activityInfor.setSecckillMoveList(secckilllist);
		activityInfor.setDateTime(new Date());
		return activityInfor;
	}

	@Override
	public void deleteOperatorSecckillByTime(String startTime) {
		Date skTime = null;
		if (startTime != null) {
			long msl = (long) Long.valueOf(startTime).longValue() * 1000;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				String str = sdf.format(msl);
				skTime = sdf.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			operatorSecckillRepository.deleteOperatorSecckillByTime(skTime);
		}

	}

	@Override
	public void updateSecckillById(String id, Integer skNum) {
		operatorSecckillRepository.updateSecckillById(id,skNum);
	}

}
